/**
 * AjLog - The AspectJ based logging facade
 * 
 * Copyright (C) 2009 - 2010, Marko Umek. All rights reserved.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.ajlog.util;

/**
 * Assert contains assert utility methods.
 * 
 * @author Marko Umek
 * 
 */
public class AjLogAssert {

    private static AjLogLogger logger = AjLogLogger.getLogger(AjLogAssert.class);

    private static class AssertationFailedException extends RuntimeException {
        private static final long serialVersionUID = 1L;

        AssertationFailedException(final String message) {
            super(message);
        }
    }

    private static class PatternMatchFailedException extends RuntimeException {
        private static final long serialVersionUID = 1L;

        public PatternMatchFailedException(final String message) {
            super(message);
        }

    }

    /**
     * Check if both object are not equal (or not the same)
     */
    public static void notEquals(final Object expected, final Object current, final String message) {
        if (expected != null && expected.equals(current)) {
            logger.fatal("'%s' == '%s' - %s", expected.toString(), AjLogStringUtils.toString(current), message);
            throw new AssertationFailedException(message);
        }
        if (expected == null && current == null) {
            logger.fatal("'%s' == '%s' - %s", AjLogStringUtils.toString(expected), AjLogStringUtils.toString(current), message);
            throw new AssertationFailedException(message);
        }

    }

    /**
     * Check if both object are equal (or the same)
     */
    public static void equals(final Object expected, final Object current, final String message) {
        if (expected != null && !expected.equals(current)) {
            logger.fatal("'%s' != '%s' - %s", expected.toString(), AjLogStringUtils.toString(current), message);
            throw new AssertationFailedException(message);
        }
        if (expected == null && current != null) {
            logger.fatal("'%s' != '%s' - %s", AjLogStringUtils.toString(expected), AjLogStringUtils.toString(current), message);
            throw new AssertationFailedException(message);
        }

    }

    /**
     * Always throws {@link AssertationFailedException}
     */
    public static void fail(final String message) {
        throw new AssertationFailedException(message);
    }

    /**
     * Checks for object not <code>null</code>.
     */
    public static void notNull(final Object object, final String message) {
        if (object == null) {
            throw new AssertationFailedException("Object must not be null: " + message);
        }
    }

    /**
     * 
     * If value does not match pattern, throw a
     * {@link PatternMatchFailedException}
     * 
     * @param value
     *            the value to check (not null)
     * @param pattern
     *            the pattern the value must match (not null)
     * 
     * @param message
     *            the message (not null)
     */
    public static void checkPattern(final String value, final String pattern, final String message) {
        notNull(value, "Value must not be null");
        notNull(pattern, "Pattern must not be null");
        if (!value.matches(pattern)) {
            logger.error("%s: Pattern '%s' does not accept '%s'", message, pattern, value);
            throw new PatternMatchFailedException(String.format("%s: Pattern '%s' does not accept '%s'", message, pattern,
                    value));
        }
    }
}
